package edu.gatech.oad;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class FilterItemsActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filter_items);
		final EditText dateText = (EditText) findViewById(R.id.dateText);
		final EditText searchText = (EditText) findViewById(R.id.searchBar);
		final Button btnFilter = (Button) findViewById(R.id.filter);
		final Button btnSearch = (Button) findViewById(R.id.btnSearchItems);
		final RadioButton lostbtn = (RadioButton) findViewById(R.id.lostRadio);
		final RadioButton fndbtn = (RadioButton) findViewById(R.id.foundRadio);

		// Category Buttons
		final RadioButton booksCategory = (RadioButton) findViewById(R.id.booksRadio);
		final RadioButton electronicsCategory = (RadioButton) findViewById(R.id.electronicsRadio);
		final RadioButton furnitureCategory = (RadioButton) findViewById(R.id.furnitureRadio);
		final RadioButton preciousCategory = (RadioButton) findViewById(R.id.preciousRadio);
		final RadioButton otherCategory = (RadioButton) findViewById(R.id.otherRadio);

		btnSearch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				MainActivity.gblFiltered = true;
				DataBaseHelper dbHelper = new DataBaseHelper(getApplicationContext());
				try {
					dbHelper.createDataBase();
					dbHelper.openDataBase();
				} catch (Exception ioe) {
					throw new Error("Unable to create database");
				}
				try {
					String input = searchText.getText().toString();
					ArrayList<Item> items = dbHelper.searchItems(input);
					System.out.println("Searched for " + input + " and returned " + items.size() + "items.");

					MainActivity.gblItemList.clear();
					for (Item i : items) {
						MainActivity.gblItemList.add(i);
					}

					dbHelper.close();
					Intent a = new Intent(FilterItemsActivity.this, ItemList.class);
					startActivity(a);

					dbHelper.close();

				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			}
		});
		btnFilter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				MainActivity.gblFiltered = true;

				String category;

				if (booksCategory.isChecked()) {
					category = "Books";
				} else if (electronicsCategory.isChecked()) {
					category = "Electronics";
				} else if (furnitureCategory.isChecked()) {
					category = "Furniture";
				} else if (preciousCategory.isChecked()) {
					category = "Precious Items";
				} else if (otherCategory.isChecked()) {
					category = "Other";
				} else {
					category = "none";
				}

				if (category == null || category.equals("")) {
					category = "none";
				}

				String date = dateText.getText().toString();
				if (date == null || date.equals("")) {
					date = "none";
				}
				try {
					String[] dateParts = date.split("-");
					date = dateParts[2] + "-" + dateParts[0] + "-" + dateParts[1];
				} catch (Exception e) {
					date = "none";
				}

				DataBaseHelper dbHelper = new DataBaseHelper(getApplicationContext());
				try {
					dbHelper.createDataBase();
					dbHelper.openDataBase();
				} catch (Exception ioe) {
					throw new Error("Unable to create database");
				}

				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				Date t = new Date();
				String today = dateFormat.format(t);

				ArrayList<Item> items = new ArrayList<Item>();
				String status = "Lost";
				if (lostbtn.isChecked()) {
					status = "Lost";
				} else if (fndbtn.isChecked()) {
					status = "Found";
				} else {
					status = "none";
				}

				items = dbHelper.getItemsBy(status, date, today, category);
				MainActivity.gblItemList = new ArrayList<Item>();
				if (items != null && items.size() != 0) {
					for (Item i : items) {
						MainActivity.gblItemList.add(i);
					}
				}
				dbHelper.close();
				Intent a = new Intent(FilterItemsActivity.this, ItemList.class);
				startActivity(a);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.filter_items, menu);
		return true;
	}

}
